package com.gsmr.util;

/*
 * 用于手动计算互调干扰
 */
public class Calintermodu {
	
	private int[] channel1;
	private int[] inf_channel;
    private double[] channel11;
    private double[] inf_channel1;
    private boolean readstate=true;
    private calintermodu_base Calintermodu = new calintermodu_base();
    private calintermodu_basefrequency Calintermodufrequency = new calintermodu_basefrequency();
    private int cal=0;
	
	private void getInputContent(String channel, String infchannel) { //整数
		try
        {
            int Num=0;
            String[] argchannel=channel.trim().split(",");
            String[] arginfchannel=infchannel.trim().split(",");
            channel1=new int[argchannel.length];
            inf_channel=new int[arginfchannel.length];
            for(int i=0;i<argchannel.length;i++)
            {
                if(argchannel[i].length()!=0)
                {
                    argchannel[i]=argchannel[i].trim();
                    if(Double.valueOf(argchannel[i])!=0)
                    {
                        channel1[Num]=Integer.valueOf(argchannel[i]);
                        Num++;
                    }
                }
            }
            Num=0;
            for(int i=0;i<arginfchannel.length;i++)
            {
                if(arginfchannel[i].length()!=0)
                {
                    arginfchannel[i]=arginfchannel[i].trim();
                    if(Double.valueOf(arginfchannel[i])!=0)
                    {
                        inf_channel[Num]=Integer.valueOf(arginfchannel[i]);
                        Num++;
                    }
                }
            }
        } catch(NumberFormatException e) {
            readstate=false;
        }
    }
	
    private void getInputContentfrequency(String channel, String infchannel) {//小数
        try
        {
            int Num=0;
            String[] argchannel=channel.trim().split(",");
            String[] arginfchannel=infchannel.trim().split(",");
            channel11=new double[argchannel.length];
            inf_channel1=new double[arginfchannel.length];
            for(int i=0;i<argchannel.length;i++)
            {
                if(argchannel[i].length()!=0)
                {
                    argchannel[i]=argchannel[i].trim();
                    if(Double.valueOf(argchannel[i])!=0)
                    {
                        channel11[Num]=Double.valueOf(argchannel[i]);
                        Num++;
                    }
                }
            }
            Num=0;
            for(int i=0;i<arginfchannel.length;i++)
            {
                if(arginfchannel[i].length()!=0)
                {
                    arginfchannel[i]=arginfchannel[i].trim();
                    if(Double.valueOf(arginfchannel[i])!=0)
                    {
                        inf_channel1[Num]=Double.valueOf(arginfchannel[i]);
                        Num++;
                    }
                }
            }
        } catch(NumberFormatException e) {
            readstate=false;
        }
    }
    
    private boolean judgechannel(int[] channel)
    {
        boolean result=true;
        int len=channel.length;
        for(int i=0;i<len;i++)
        {
            if((channel[i]>124&&channel[i]<999)||channel[i]>1024||channel[i]<0)
            {
                readstate=false;
                result=false;
                break;
            }
        }
        return result;
    }
    
    private boolean judgefrequency(double[] channel)
    {
        boolean result=true;
        int len=channel.length;
        for(int i=0;i<len;i++)
        {
            if((channel[i]>915&&channel[i]<930)||channel[i]>960||channel[i]<885)
            {
                readstate=false;
                result=false;
                break;
            }
        }
        return result;
    }
    
    /**
	 * 修改AlarmConfig
	 */
	public String calintermodu(String state, String channel, String infchannel){
		String Return="";    
        String result="";
        String result1="";
        
        if (state == null) {
        	state = "0";
        	System.out.println("手动计算互调干扰[输入模式]不能为空");
		} else if (channel == null) {
			throw new RuntimeException("手动计算互调干扰[干扰信道号/频率]不能为空");
		} else if (infchannel == null) {
			throw new RuntimeException("手动计算互调干扰[被干扰信道号/频率]不能为空");
		}
        
        if(state.equals("0"))
        {
        	getInputContent(channel,infchannel);
            if(!judgechannel(channel1))
            {
            	return Return;
            }
            if(!judgechannel(inf_channel))
            {
            	return Return;
            }
        }
        if(state.equals("1"))
        {
            getInputContentfrequency(channel,infchannel);
            if(!judgefrequency(channel11))
            {
            	return Return;
            }
            if(!judgefrequency(inf_channel1))
            {
            	return Return;
            }
        }
        if(readstate)
        {
            if(state.equals("0"))
            {
                if(inf_channel.length==0)
                {
                	return Return;
                }
                else
                {
                    if(channel1.length<2)
                    {
                    	cal=1;
                    }
                    else if(channel1.length==2)
                    {
                    	
                        for(int i=0;i<inf_channel.length;i++)
                        {
                            result=result+Calintermodu.getPossibleInfChannel(channel1[0],channel1[1],inf_channel[i]);
                            result1=result1+Calintermodu.getPossibleInfChanneldown(channel1[0],channel1[1],inf_channel[i]);
                        }
                    }
                    else
                    {
                        for(int i1=0;i1<inf_channel.length;i1++)
                        {
                            for(int i2=0;i2<channel1.length;i2++)
                            {
                                for(int i3=i2+1;i3<channel1.length;i3++)
                                {
                                    result=result+Calintermodu.getPossibleInfChannel(channel1[i2],channel1[i3],inf_channel[i1]);
                                    result1=result1+Calintermodu.getPossibleInfChanneldown(channel1[i2],channel1[i3],inf_channel[i1]);
                                }
                            }
                        }
                        for(int i1=0;i1<inf_channel.length;i1++)
                        {
                            for(int i2=0;i2<channel1.length;i2++)
                            {
                                for(int i3=i2+1;i3<channel1.length;i3++)
                                {
                                    for(int i4=i3+1;i4<channel1.length;i4++)
                                    {
                                        result=result+Calintermodu.getPossibleInfChannel(channel1[i2],channel1[i3],channel1[i3],inf_channel[i1]);
                                        result1=result1+Calintermodu.getPossibleInfChanneldown(channel1[i2],channel1[i3],channel1[i3],inf_channel[i1]);
                                    }
                                }
                            }
                        }  
                    }
                    if((result1.length()==0)&&(result.length()==0)&&cal==0)
                    {
                        Return="没有互调干扰!";
                    }
                    if(result.length()!=0||result1.length()!=0)
                    {
                        if(result.length()!=0)
                        {
                            Return=Return+"上行信道存在互调干扰："+result;
                        }
                        if(result1.length()!=0)
                        {
                        	Return=Return+"下行信道存在互调干扰："+result1;
                        }
                    }
                    cal=0;
                }
            }
            if(state.equals("1"))
            {
                if(inf_channel1.length==0)
                {
                	return Return;
                }
                else
                {
                    if(channel11.length<2)
                    {
                        cal=1;
                    }
                    else if(channel11.length==2)
                    {
                        for(int i=0;i<inf_channel1.length;i++)
                        {
                            result=result+Calintermodufrequency.getPossibleInfChannel(channel11[0],channel11[1],inf_channel1[i]);
                        }
                    }
                    else
                    {
                        for(int i1=0;i1<inf_channel1.length;i1++)
                        {
                            for(int i2=0;i2<channel11.length;i2++)
                            {
                                for(int i3=i2+1;i3<channel11.length;i3++)
                                {
                                    result=result+Calintermodufrequency.getPossibleInfChannel(channel11[i2],channel11[i3],inf_channel1[i1]);
                                }
                            }
                        }
                        for(int i1=0;i1<inf_channel1.length;i1++)
                        {
                            for(int i2=0;i2<channel11.length;i2++)
                            {
                                for(int i3=i2+1;i3<channel11.length;i3++)
                                {
                                    for(int i4=i3+1;i4<channel11.length;i4++)
                                    {
                                        result=result+Calintermodufrequency.getPossibleInfChannel(channel11[i2],channel11[i3],channel11[i3],inf_channel1[i1]);
                                    }
                                }
                            }
                        }  
                    }
                    if(result.length()==0&&cal==0)
                    {
                        Return="没有互调干扰!";
                    }
                    if(result.length()!=0)
                    {
                        Return=result;
                    }
                    cal=0;
                }
            }
        }
        else
            readstate=true;
		return Return;
	}
}